package com.yidu.logisticssystem.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.vo.MapVo;
import com.yidu.logisticssystem.service.LogisticsMapService;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.tools.Tool;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地区表(LogisticsMap)表控制层
 *
 * @author 黄翔
 * @since 2021-03-08 16:46:50
 */
@Api(description= "地区接口")
@RestController
@RequestMapping("/logisticsMap")
public class LogisticsMapController {

    @Resource
    RedisTemplate redisTemplate;
    /**
     * 服务对象
     */
    @Resource
    private LogisticsMapService logisticsMapService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsMap selectOne(String id) {
        return this.logisticsMapService.queryById(id);
    }


    /**
     * 查询所有下级地区
     * @return BaseMessage
     */
    @GetMapping("selectzifing")
    public List<MapVo> selectzifing(){
        List<MapVo> map=(List<MapVo>) redisTemplate.opsForValue().get("map");
        if (map==null){
            map=this.logisticsMapService.selectzifind("0");
            redisTemplate.opsForValue().set("map",map);
        }
        return map;
    }

    /**
     * 分页查询所有
     * @param page
     * @param limit
     * @return
     */

    @ApiOperation(value = "查询所有的地区",notes = "返回参数")
    @GetMapping("findAll")
    public Map<String,Object> findAll(LogisticsMap logisticsMap, int page, int limit){
        //将参数传入分页助手 PageHelper.startPage
        PageHelper.startPage(page,limit);
        Map<String,Object> map = new HashMap<>();
        List<LogisticsMap> list =logisticsMapService.queryAll(logisticsMap);
        PageInfo<LogisticsMap> pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","");
        map.put("data",pageInfo.getList());//所有数据
        map.put("count",pageInfo.getTotal());//总行数
        return map;
    }



    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除",notes = "返回参数")
    @PostMapping("/delete/{id}")
    public BaseMessage delete(@PathVariable int id){
        boolean flag=logisticsMapService.deleteById(id);

        if(flag){
            return BaseMessage.succes();
        }else{
            return BaseMessage.fail();
        }
    }



    @ApiOperation(value = "根据id查找上级",notes = "返回参数")
    @GetMapping("selectfuid/{id}")
        public BaseMessage selectfufing(@PathVariable String id){
        List<LogisticsMap> list=logisticsMapService.selectfufind(id);

        if(!list.isEmpty()){
            return BaseMessage.succes(list);
        }else{
            return BaseMessage.fail();
        }
    }



    /**
     * 新增和修改
     * @param logisticsMap
     * @return
     */
    @ApiOperation(value = "修改和添加功能实现",notes = "返回参数")
    @PostMapping("addAndUpdate")
    public BaseMessage addAndUpdate(LogisticsMap logisticsMap){
        BaseMessage baseMessage = new BaseMessage();
        if(logisticsMap.getMapId()==null){
            logisticsMapService.insert(logisticsMap);
            baseMessage.setState(1);
            baseMessage.setMessage("增加成功");
        }else{
            /**
             * 修改数据
             */
            logisticsMapService.update(logisticsMap);
            baseMessage.setState(1);
            baseMessage.setMessage("修改成功");
        }
        return baseMessage;
    }


    /**
     * 新增和修改
     * @param mapId
     * @return
     */
    @ApiOperation(value = "显示",notes = "返回参数")
    @PostMapping("showUpdate")
    public BaseMessage showUpdate(String mapId){
        BaseMessage baseMessage = new BaseMessage();
            /**
             * 在页面上显示单条数据
             */
            LogisticsMap logisticsMap1 = logisticsMapService.queryById(mapId);

            Map<String,Object> map = new HashMap<>();
            map.put("code",0);
            map.put("msg","");
            map.put("data",logisticsMap1);//所有数据
            map.put("count","");//总行数

        return baseMessage;
    }
}
